<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Qh_OrderRequest;
use App\Models\Order;
use App\Models\Qh_Cdkey;
use App\Models\Qh_Order;
use App\Service\Wechat\WechatPayService;
use DB;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class Qh_OrderController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = Qh_Order::filter(\request()->all())
            ->select([
                'tiku_order.*',
                'user.id as userId',
                'user.nickname as userNickname',
                'user.avatar as userAvatar',
                'user.mobile as userMobile',
                'user.status as userStatus'
            ])
            ->join('user', 'tiku_order.userId', '=', 'user.id')
            ->orderByDesc('id')
            ->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Qh_OrderRequest $request
     * @return JsonResponse|object
     */
    public function store(Qh_OrderRequest $request)
    {
        return $this->restful();
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function show(int $id)
    {
        return $this->restful();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Qh_OrderRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(Qh_OrderRequest $request, int $id)
    {
        return $this->restful();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     * @throws \Throwable
     */
    public function destroy(int $id)
    {
        $order = Qh_Order::findOrFail($id);

        try {
            DB::beginTransaction();
            // 删除绑定的激活码
            if ($order->type == 2) {
                $cdkey = Qh_Cdkey::where('orderId', $order->id)->where('userId', $this->user->id)->first();
                $cdkey->delete();
            }

            $order->delete();

            DB::commit();
        }catch (Exception $e) {
            DB::rollBack();
            throw new Exception('删除失败');
        }

        return $this->restful();
    }

    /**
     * 退款申请
     * @param $id
     * @param WechatPayService $wechatPayService
     * @return JsonResponse|object
     * @throws InvalidConfigException
     * @throws Exception
     */
    public function refund($id, WechatPayService $wechatPayService) {
        $order = Order::findOrFail($id);
        if ($order->status != 2) $this->restful(true, true, '该状态不支持退款');

        $refundTradeNo = 'REFUND'.time().rand(1000, 9999);
        $order->refundTradeNo = $refundTradeNo;
        $order->save();

        $result = $wechatPayService->refund($order);

        if (!$result) {
            return $this->restful(null, false, '退款申请失败');
        }

        return $this->restful(true, true, '退款申请成功');
    }
}
